// 中介
class Agency {
  _topics = {}
  subscribe(topic, listener) {
    const listeners = this._topics[topic]
    if (listeners) {
      listeners.push(listener)
    } else {
      this._topics[topic] = [listener]
    }
  }
  publish(topic, ...args) {
    const listeners = this._topics[topic] || []
    listeners.forEach(listener => listener(...args))
  }
}

// 房东
class Landlord {
  constructor(public agent: Agency) { }
  lend(topic, area, money) {
    this.agent.publish(topic, area, money)
  }
}

class Tenant {
  constructor(public agent: Agency, public name: string) { }
  order(topic) {
    this.agent.subscribe(topic, (area, money) => {
      console.log(this.name, `${area}平米, ${money}元`)
    })
  }
}

const agent = new Agency()
const rich = new Tenant(agent, '大款')
const poor = new Tenant(agent, '北漂')
const landlord = new Landlord(agent)
rich.order('豪宅')
poor.order('单间')
landlord.lend('豪宅', 10000, 1000000)
landlord.lend('单间', 10, 1000)
